package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;

final class SubtractActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (!(source instanceof Number)) {
            return null;
        }
        if (args.length == 0) {
            BigDecimal r = new BigDecimal(String.valueOf(source)).multiply(BigDecimal.valueOf(-1));
            if (source instanceof Double) {
                return r.doubleValue();
            } else if (source instanceof Float) {
                return r.floatValue();
            } else if (source instanceof Long) {
                return r.longValue();
            }
            return r.intValue();
        }
        Object subtrahend = args[0];
        if (!(subtrahend instanceof Number)) {
            return null;
        }
        if ((source instanceof Double) || (subtrahend instanceof Double)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Double.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(subtrahend, Double.class))).doubleValue();
        }
        if ((source instanceof Float) || (subtrahend instanceof Float)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Float.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(subtrahend, Float.class))).floatValue();
        }
        if ((source instanceof Long) || (subtrahend instanceof Long)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Long.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(subtrahend, Long.class))).longValue();
        }
        return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Integer.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(subtrahend, Integer.class))).intValue();
    }
}
